/*
 * @Author: Ma Xiaohan
 * @Date: 2018-08-19 12:36:46
 * @Last Modified by: Xiaohan Ma
 * @Last Modified time: 2018-09-01 15:56:45
 */
import {omit} from '../utils/utils';
const BasicModel = require('./BasicModel');

module.exports = class extends BasicModel {
  get relation() {
    return {
      introduction_files: {
        type: think.Model.HAS_MANY,
        model: 'introduction_files',
        key: 'id',
        fKey: 'relation_id'
      },
      richtext: {
        type: think.Model.BELONG_TO,
        model: 'richtext',
        key: 'id',
        fKey: 'relation_id'
      }
    };
  }
  async addAction(params) {
    const id = think.uuid('v4');
    params.data.id = id;
    const data = await this.thenAdd(params.data, {title: params.data.title});
    const richTextData = {
      data: {}
    };
    richTextData.data.relation_id = id;
    richTextData.data.richtext = '';
    const richtext = await this.model('richtext').addAction(richTextData);
    if (data.type === 'add') {
      return {...richtext, ...data, id};
    }
    return {...richtext, ...data};
  }

  async editAction(params) {
    const finddata = await this.model(params.model).where({ id: params.data.id }).find();
    if (think.isEmpty(finddata)) {
      const msg = '数据不存在，无法修改';
      return {msg};
    }
    const richTextData = {
      data: {}
    };
    richTextData.data.richtext = params.data.richtext;
    richTextData.data.relation_id = params.data.id;
    const richtext = await this.model('richtext').editAction(richTextData);
    delete params.richtext;
    const data = this.model(params.model).where({ id: params.data.id }).update(params.data);
    return {...richtext, ...data};
  }

  async delAction(params) {
    const richTextData = await this.model('richtext').where({relation_id: params.id}).delete();
    const filesinfo = await this.model('recruit_enclosure').where({relation_id: params.id}).delete();
    // 删除文件夹及文件夹下所有文件
    // const fileDirPath = `/home/mdalang/projects/mobile_internet_website_server/runtime/upload/projectFileInfo/${params.id}`;
    const fileDirPath = `/var/www/html/mobile_internet_website_server/runtime/upload/projectFileInfo/${params.id}`;
    await think.rmdir(fileDirPath, false);
    const recruitNewInfoData = await this.model(params.model).where(params.data).delete();
    return {recruitNewInfoData, richTextData, filesinfo};
  }

  async getAction(params) {
    // 数据转换
    const data = await this.model(params.model).where({id: params.id}).find();
    const richText = await this.model('richtext').where({relation_id: params.id}).find();
    const recruitEnclosure = await this.model('recruit_enclosure').where({relation_id: params.id}).select();
    // blob数据转string返回前端
    let richtext = richText.richtext;
    if (think.isEmpty(richtext)) {
      richtext = '';
    }
    richtext = richtext.toString('utf-8');
    data.richtext = richtext;
    data.recruit_enclosure = recruitEnclosure;
    return data;
  }

  async listpageAction(params) {
    const searchData = omit(params.data, ['page', 'pageSize'], true);
    const key = Object.keys(searchData)[0];
    const value = searchData[key];
    // 去查询条件的第一个进行模糊查询
    let fuzzyQueryData = {};
    if (think.isEmpty(key)) {
      fuzzyQueryData = {};
    } else {
      fuzzyQueryData[key] = ['like', `%${value}%`];
    }
    const { page = 1, pageSize = 20 } = params.data;
    const dataList = await this.order('create_time DESC').where(fuzzyQueryData).page(page, pageSize).countSelect();
    const {
      currentPage: current,
      totalPages: pages,
      count: total,
      data
    } = dataList;
    return { pageSize, pages, current, total, data };
  }

  async listallAction(params) {
    let data;
    var arr = Object.keys(params.data);// 传空返回全部数据，不为空选择查询
    if (arr.length) {
      data = await this.model(params.model).where({...params.data}).order('create_time DESC').select();
      return data;
    } else {
      data = await this.model(params.model).order('create_time DESC').select();
      return data;
    }
  }

  // 前台展示接口
  async getlistAction() {
    return this.order('create_time DESC').select();
  }
};
